unit LogFile;

interface

uses
 gnugettext,
 JvLogFile;

const
 cTimeExit = 0;
 cTime60 = 1;
 cTime10 = 2;
 cTime05 = 3;
 cTimeEvery = 4;

function IsBitSet(Value, BitPlace: Integer): Boolean;
function IsBitsSet(Value: Integer; const Bits: array of const): Boolean;
//
procedure LogCreate(LoadFrom: String; anAutoSave: Boolean = false);
procedure LogSave(SaveTo: String = '');
procedure LogShow(Title: String);
procedure LogFree;
function LogActive: Boolean;
procedure Log(AReason, AText: String; TimeOnly: Boolean = false);
procedure LogSize(const Size: Cardinal);

implementation

uses
 SysUtils,
 StringUtils;

var
 myLogFile: TJvLogFile = nil;

 {Log operation routines}

function IsBitSet(Value, BitPlace: Integer): Boolean;
begin
 Result:=(Value and BitPlace)>0;
end;

function IsBitsSet(Value: Integer; const Bits: array of const): Boolean;
 var
  i: Integer;
begin
 Result:=true;
 for i:=0 to High(Bits) do
  begin
   if Bits[i].VType=vtInteger then
     Result:=Result and IsBitSet(Value, Bits[i].VInteger);
  end;
end;

procedure LogCreate(LoadFrom: String; anAutoSave: Boolean = false);
begin
 LogFree;
 if LoadFrom<>'' then
  begin
   myLogFile:=TJvLogFile.Create(nil);
   myLogFile.FileName:=LoadFrom;
   myLogFile.AutoSave:=anAutoSave;
  end;
end;

procedure LogSave(SaveTo: String = '');
begin
 if LogActive then
  begin
   if SaveTo='' then
     myLogFile.SaveToFile(myLogFile.FileName)
   else
     myLogFile.SaveToFile(SaveTo);
  end;
end;

procedure LogShow(Title: String);
begin
 if LogActive then
   myLogFile.ShowLog(Title);
end;

procedure LogFree;
begin
 if LogActive then
  begin
   FreeAndNil(myLogFile);
  end;
end;

function LogActive: Boolean;
begin
 Result:=Assigned(myLogFile);
end;

procedure Log(AReason, AText: String; TimeOnly: Boolean = false);
 var
  LogText: String;
begin
 if LogActive then
  begin
   LogText:=AText;
   ReplaceEx(LogText, #13, '_');
   ReplaceEx(LogText, #10, '_');
   ReplaceEx(LogText, '<', '(');
   ReplaceEx(LogText, '>', ')');
   if TimeOnly then
     myLogFile.Add(TimeToStr(Time), AReason, LogText)
   else
     myLogFile.Add(DateTimeToStr(Now), AReason, LogText);
  end;
end;

procedure LogSize(const Size: Cardinal);
begin
 if LogActive then
   myLogFile.SizeLimit:=Size;
end;

end.

